package com.base.common.model.po;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 用于分页查询传输的数据对象
 *
 * @author admin
 * @date 2020/5/12 PM2:11
 */
@Data
@NoArgsConstructor
@ApiModel(value = "PageBase", description = "分页对象")
public class PageBasePo<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * ASC排序取值
     */
    public static final String ASC = "asc";
    /**
     * DESC排序取值
     */
    public static final String DESC = "desc";
    /**
     * 默认每页展示数据条数，取值为20
     */
    public static final int DEFAULT_MAX_SIZE = 20;

    /**
     * 单页面最大查询数据条数，取值为1000
     */
    public static final int MAX_SIZE = 1000;

    /**
     * 分页查询参数：查询页码，默认取值为1，即查询第一页
     */
    @ApiModelProperty(notes = "查询页码")
    protected Integer current = 1;

    /**
     * 分页查询参数：查询页面数据条数，默认取值为MAX_SIZE
     */
    @ApiModelProperty(notes = "每页条数")
    protected Integer size = DEFAULT_MAX_SIZE;

    /**
     * 查询结果：获取到的数据集合
     */
    @ApiModelProperty(notes = "数据集合")
    protected List<T> result;

    /**
     * 查询结果：获取到的总记录数
     */
    @ApiModelProperty(notes = "总记录数")
    protected Integer totalCount;

    /**
     * 总页数
     */
    @ApiModelProperty(notes = "总页数")
    private Integer totalPage;

    public PageBasePo(Integer current, Integer size) {
        this.current = current;
        this.size = size;
    }


    /**
     * 分页
     *
     * @param result     列表数据
     * @param totalCount 总记录数
     * @param size       每页记录数
     * @param current    当前页数
     */
    public PageBasePo(List<T> result, Integer totalCount, Integer size, Integer current) {
        this.result = result;
        this.totalCount = totalCount;
        this.size = size;
        this.current = current;
        this.totalPage = (int) Math.ceil((double) totalCount / size);
    }

    /**
     * 分页
     */
    public PageBasePo(IPage<T> page) {
        this.result = page.getRecords();
        this.totalCount = (int) page.getTotal();
        this.size = (int) page.getSize();
        this.current = (int) page.getCurrent();
        this.totalPage = (int) page.getPages();
    }
}
